<template>
  <div class="index">
    <div class="title-box">
      <div class="title">
        <div>ToDoList</div>
        <el-input
          placeholder="添加todo"
          v-model.trim="form.todo"
          @keyup.enter.native="onAdd"
        ></el-input>
      </div>
    </div>

    <div class="box">
      <div class="to-do-tasks">
        <div class="top">
          <div class="left">正在进行</div>
          <div class="right">{{ todoList.length || 0 }}</div>
        </div>
        <div
          class="to-do-item"
          v-for="(item, index) in todoList"
          :key="index + item.label"
        >
          <el-checkbox
            v-model="item.isDone"
            @change="onEdit"
            >{{ item.label }}</el-checkbox
          >
          <el-button
            type="warning"
            @click="onDelete(index)"
            size="small"
          >
            删除</el-button
          >
        </div>
      </div>

      <div class="done">
        <div class="top">
          <div class="left">已经完成</div>
          <div class="right">{{ doneList.length || 0 }}</div>
        </div>

        <div
          class="to-do-item"
          v-for="(item, index) in doneList"
          :key="index + item.label"
        >
          <el-checkbox
            v-model="item.isDone"
            @change="onEdit"
            >{{ item.label }}</el-checkbox
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TodoList', // 代办列表
  components: {},
  watch: {},
  computed: {
    todoList() {
      return this.list.filter(v => !v.isDone)
    },

    doneList() {
      return this.list.filter(v => v.isDone)
    },
  },
  data() {
    //这里存放数据
    return {
      form: {
        todo: '',
      },

      list: [],
    }
  },

  methods: {
    onAdd() {
      if (!this.form.todo) {
        this.$message.error('不能添加为空的待办事项')
        return
      }

      const obj = {
        label: this.form.todo,
        isDone: false,
      }

      this.list.unshift(obj)
      this.form.todo = ''
      this.setLocalStorage('todoList', JSON.stringify(this.list))
    },

    onEdit() {
      console.log('触发了编辑----')
      this.setLocalStorage('todoList', JSON.stringify(this.list))
    },

    onDelete(index) {
      this.list.splice(index, 1)
      this.setLocalStorage('todoList', JSON.stringify(this.list))
    },

    setLocalStorage(key, value) {
      localStorage.setItem(key, value)
    },

    getLocalStorage(key) {
      return JSON.parse(localStorage.getItem(key))
    },

    init() {
      this.list = this.getLocalStorage('todoList') || []
    },
  },
  created() {
    this.init()
  },
  mounted() {},
}
</script>
<style
  lang="scss"
  scoped
>
.index {
  // margin: 50px auto;
  height: 100vh;
  background: gray;

  .title-box {
    display: flex;
    justify-content: center;
    background: #333;
    margin-bottom: 20px;

    .title {
      width: 800px;
      font-size: 28px;
      padding: 0 10px;
      height: 60px;

      color: #fff;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  .box {
    margin: 50px auto;
    width: 800px;

    .to-do-tasks {
      .top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 5px;

        .left {
          font-size: 28px;
          font-weight: 600;
        }

        .right {
          font-size: 28px;
          font-weight: 600;
        }
      }

      .to-do-item {
        height: 60px;
        padding: 0 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        margin-bottom: 10px;
      }
    }

    .done {
      .top {
        margin-bottom: 5px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .left {
          font-size: 28px;
          font-weight: 600;
        }

        .right {
          font-size: 28px;
          font-weight: 600;
        }
      }

      .to-do-item {
        height: 60px;
        padding: 0 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        margin-bottom: 10px;
      }
    }
  }
}
</style>
